package com.lmxdawn.api.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmxdawn.api.admin.dao.MdMapper;
import com.lmxdawn.api.admin.entity.Md;
import com.lmxdawn.api.admin.service.IMdService;
import com.lmxdawn.api.common.api.Constants;
import com.lmxdawn.api.common.exception.NotRealnameException;
import com.lmxdawn.api.common.session.SessionContextHolder;
import com.lmxdawn.api.common.util.IdWorker;
import com.lmxdawn.api.dnf.dto.SaleReq;
import com.lmxdawn.api.dnf.entity.Order;
import com.lmxdawn.api.dnf.entity.OrderItem;
import com.lmxdawn.api.dnf.mapper.OrderItemMapper;
import com.lmxdawn.api.dnf.mapper.OrderMapper;
import lombok.val;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangyubin
 * @since 2024-07-04
 */
@Service
public class MdServiceImpl extends ServiceImpl<MdMapper, Md> implements IMdService {

    @Resource
    private MdMapper mdMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderItemMapper orderItemMapper;
    @Resource
    private IdWorker idWorker;


    @Override
    public void resumeStockNum(Long productId, BigDecimal quantityOrder) {
        mdMapper.resumeStockNum(productId, quantityOrder);
    }

    @Override
    public void deductStockNum(Long productId, BigDecimal quantity) {
        mdMapper.discountStockNum(productId, quantity);
    }

    @Override
    public String doSale(SaleReq content, String id) {
        val goldCoin = new Md();
        if (!SessionContextHolder.get().isRealName()) {
            throw new NotRealnameException();
        }
        if (content.getUnit() == null || content.getUnit().equals("元")){
            // 单次出售多少元 比例= 多少
            goldCoin.setQuantity(content.getQuantity().toString());
        }else{
            goldCoin.setQuantity(BigDecimal.ONE.divide(content.getQuantity(), 2 , RoundingMode.HALF_UP).toString());
        }
        goldCoin.setAmount(content.getAmount().toString());
        goldCoin.setPrice(content.getPrice().toString());
        goldCoin.setGameServer(content.getGameServer());
        goldCoin.setType(content.getType() == 2 ? 2 : 15);
        goldCoin.setCharge("0");
        goldCoin.setSaleUser(id.toString());
        goldCoin.setPlatform(content.getPlatform());
        goldCoin.setValidHour(content.getValidHour());
        goldCoin.setContact(content.getContact());
        goldCoin.setOnlineTime(content.getOnlineTime());
        goldCoin.setUnit(content.getUnit());
        goldCoin.setAuction(content.getAuction());
        if (content.getId() == null){
            mdMapper.insert(goldCoin);

            if (Arrays.asList( 4 , 12 ,14).contains(content.getType())){
                Order order = new Order();
//                GoldCoin coin = this.lambdaQuery().eq(GoldCoin::getType, content.getType() - 1)
//                        .eq(GoldCoin::getGameServer, content.getGameServer().split("-")[0])
//                        .last("limit 1").one();
                //设置订单id
                order.setId(idWorker.orderNo("M"));
                order.setGameServer(content.getGameServer());
                order.setPhone(content.getContact());
                order.setPaymentMethod(3);//商城
                order.setTotalAmount(content.getQuantity());
                order.setStatus(Constants.ZERO);
                order.setPaymentStatus(Constants.ONE);
                order.setUserId("user-0");
                order.setCreateTime(new Date());
                order.setType(goldCoin.getType());
                order.setSaleId(goldCoin.getSaleUser());
                order.setPayAccount(null);
                order.setNotes("用户出售，用户id:"+SessionContextHolder.getId());
                //新增订单数据
                OrderItem orderItem = new OrderItem();
                orderItem.setId(idWorker.nextId());
                orderItem.setProductId(goldCoin.getId());
                orderItem.setQuantityOrder(content.getQuantity());
                orderItem.setPrice(content.getPrice());
                orderItem.setOrderId(order.getId());
                StringBuilder sb = new StringBuilder();
                sb.append("矛盾");
                sb.append(" : ");
                sb.append(orderItem.getPrice());
                sb.append("(个/元)");
                sb.append(" * ");
                sb.append(orderItem.getQuantityOrder());
                order.setRemark(sb.toString());
                order.setRate(orderItem.getPrice());
                orderMapper.insertOrder(order);
                orderItemMapper.insertItem(orderItem);
                return order.getId().toString();
            }
        }else{
            goldCoin.setId(content.getId());
            goldCoin.setCreateTime(new Date());
            mdMapper.updateById(goldCoin);
        }


        return null;
    }
}
